<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>chat</title>
    <script src="../resources/js/realtime.browser.js"></script>
    <script src="../resources/js/jquery.js"></script>
    <script>
        var Realtime = AV.Realtime;
        var TextMessage = AV.TextMessage;
        var realtime = new Realtime({
            appId: 'HGbJCv3icHfp9iiNF1Jy18Fd-gzGzoHsz',
            appKey: 'femookwAm3OajFthgKGT3NDO',
            region: 'cn', //美国节点为 "us"
        });
        // Tom 用自己的名字作为 clientId，获取 IMClient 对象实例
        var TomClient = realtime.createIMClient('Tom')
        var tomConversation = TomClient.then(function (tom) {
            // 创建与Jerry之间的对话
            return tom.createConversation({
                members: ['Jerry'],
                name: 'Tom & Jerry',
            });
        })
        tomConversation.then(function (conversation) {
                // 发送消息
                return conversation.send(new AV.TextMessage('耗子，起床！'));
            })
            .then(function (message) {
                console.log('Tom & Jerry', '发送成功！');
            }).catch(console.error);

        var jerryClient = realtime.createIMClient('Jerry').then(function (jerry) {
            jerry.on('message', function (message, conversation) {
                console.log('Message received: ' + message.text);
            });
        }).catch(console.error);
        $(function () {
            $("#sendMessage").click(function () {
                tomConversation.then(function (conversation) {
                    // 发送消息
                    return conversation.send(new AV.TextMessage('耗子，在起床！'));
                });
            });
        })
    </script>
</head>
<body>
<h1>Chat</h1>
<button id="sendMessage">sendMessage</button>
</body>
</html>